﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PL_TileEditor
{
    public partial class Form1 : Form
    {
        public MapViewer.Tool SavedTool = MapViewer.Tool.Pen;

        public Form1()
        {
            InitializeComponent();

            mapViewer1.Tiles = tileViewer1;
            //tileViewer1.TileImage = (Bitmap)(Bitmap.FromFile("DefaultSet.png"));
            //tileViewer1.ChangeImage("Tiles01.png");
            mapViewer1.AutoScrollMinSize = new Size(32 * 64, 32 * 64);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            mapViewer1.ChangeSize(new Size((int)numericUpDown1.Value, mapViewer1.MapSize.Height));
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            mapViewer1.ChangeSize(new Size(mapViewer1.MapSize.Width,(int)numericUpDown2.Value));
        }

        private void loadTileSheetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tileViewer1.ChangeImage(openFileDialog1.FileName);
            }
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            tileViewer1.ChangeTileSize((int)numericUpDown3.Value, (int)numericUpDown3.Value);
            tileViewer1.Invalidate();
            mapViewer1.Invalidate();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            tileViewer1.TileProperties[tileViewer1.SelectedTile].IsSolid = checkBox1.Checked;
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            if (numericUpDown4.Value >= 0 && numericUpDown4.Value < 3)
            {
                mapViewer1.SelectedLayer = (int)numericUpDown4.Value;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            mapViewer1.ViewableLayers[0] = checkBox2.Checked;
            mapViewer1.Invalidate();
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            mapViewer1.ViewableLayers[1] = checkBox3.Checked;
            mapViewer1.Invalidate();
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            mapViewer1.ViewableLayers[2] = checkBox4.Checked;
            mapViewer1.Invalidate();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream tempFile;
                tempFile = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(tempFile);
                bw.Write(1);
                bw.Write(Path.GetFileName(tileViewer1.TileImageFile));
                bw.Write(tileViewer1.TileImage.Width / tileViewer1.TileSize.Width);
                bw.Write(tileViewer1.TileImage.Height / tileViewer1.TileSize.Height);
                bw.Write(tileViewer1.TileSize.Width);
                bw.Write(tileViewer1.TileSize.Height);
                for (int i = 0; i < tileViewer1.TileProperties.Count; i++)
                {
                    bw.Write(tileViewer1.TileProperties[i].IsSolid);
                }

                bw.Write(mapViewer1.MapSize.Width);
                bw.Write(mapViewer1.MapSize.Height);
                int numTiles = 0;
                for (int z = 0; z < 3; z++)
                {
                    for (int x = 0; x < mapViewer1.MapSize.Width; x++)
                    {
                        for (int y = 0; y < mapViewer1.MapSize.Height; y++)
                        {
                            if (mapViewer1.TileMap[z, x, y] != 9999)
                            {
                                numTiles += 1;
                            }
                        }
                    }
                }
                bw.Write(numTiles);
                for (int z = 0; z < 3; z++)
                {
                    for (int x = 0; x < mapViewer1.MapSize.Width; x++)
                    {
                        for (int y = 0; y < mapViewer1.MapSize.Height; y++)
                        {
                            if (mapViewer1.TileMap[z, x, y] != 9999)
                            {
                                bw.Write(x);
                                bw.Write(y);
                                bw.Write(z);
                                bw.Write(mapViewer1.TileMap[z, x, y]);
                            }
                        }
                    }
                }
                bw.Write(mapViewer1.ObjectList.Count);
                for (int i = 0; i < mapViewer1.ObjectList.Count; i++)
                {
                    if (mapViewer1.ObjectList[i].GetType() == "Portal")
                    {
                        bw.Write("Portal");
                        bw.Write(mapViewer1.ObjectList[i].m_Position.X);
                        bw.Write(mapViewer1.ObjectList[i].m_Position.Y);
                        bw.Write(((Portal)mapViewer1.ObjectList[i]).m_EndPosition.X);
                        bw.Write(((Portal)mapViewer1.ObjectList[i]).m_EndPosition.Y);
                        bw.Write(((Portal)mapViewer1.ObjectList[i]).m_szEnd);
                        bw.Write(((Portal)mapViewer1.ObjectList[i]).m_TriggerButton);
                    }
                    else if (mapViewer1.ObjectList[i].GetType() == "Enemy")
                    {
                        bw.Write("Enemy");
                        bw.Write(((Enemy)mapViewer1.ObjectList[i]).Filename);
                        bw.Write(mapViewer1.ObjectList[i].m_Position.X);
                        bw.Write(mapViewer1.ObjectList[i].m_Position.Y);
                    }
                }
                tempFile.Close();

            }
        }

        private void tileViewer1_MouseUp(object sender, MouseEventArgs e)
        {
            checkBox1.Checked = tileViewer1.TileProperties[tileViewer1.SelectedTile].IsSolid;
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                FileStream tempFile = new FileStream(openFileDialog2.FileName, FileMode.Open);
                BinaryReader br = new BinaryReader(tempFile);
                int Version = br.ReadInt32();
                int NameLength = br.Read();
                char[] TileImage = new char[NameLength];
                br.Read(TileImage, 0, NameLength);
                tileViewer1.ChangeImage("../tiles/" + new string(TileImage));
                Size TileImageSize = new Size(br.ReadInt32(), br.ReadInt32());
                Size TileSize = new Size(br.ReadInt32(), br.ReadInt32());
                numericUpDown3.Value = TileSize.Width;
                tileViewer1.ChangeTileSize(TileSize.Width, TileSize.Height);
                for (int i = 0; i < TileImageSize.Width * TileImageSize.Height; i++)
                {
                        tileViewer1.TileProperties[i].IsSolid = br.ReadBoolean();
                }
                tileViewer1.SelectedTile = 0;
                checkBox1.Checked = tileViewer1.TileProperties[tileViewer1.SelectedTile].IsSolid;
                Size MapSize = new Size(br.ReadInt32(), br.ReadInt32());
                numericUpDown1.Value = MapSize.Width;
                numericUpDown2.Value = MapSize.Height;
                mapViewer1.ChangeSize(MapSize);
                for (int z = 0; z < 3; z++)
                {
                    for (int x = 0; x < MapSize.Width; x++)
                    {
                        for (int y = 0; y < MapSize.Height; y++)
                        {
                            mapViewer1.TileMap[z, x, y] = 9999;
                        }
                    }
                }
                int NumTiles = br.ReadInt32();
                for (int i = 0; i < NumTiles; i++)
                {
                    int x = br.ReadInt32();
                    int y = br.ReadInt32();
                    int z = br.ReadInt32();
                    mapViewer1.TileMap[z, x, y] = br.ReadInt32();
                }
                mapViewer1.ObjectList.Clear();
                int NumObjects = br.ReadInt32();
                for (int i = 0; i < NumObjects; i++)
                {
                    string TypeOfObject = br.ReadString();
                    if (TypeOfObject == "Portal")
                    {
                        Portal TempPortal = new Portal();
                        TempPortal.m_Position = new Point(br.ReadInt32(), br.ReadInt32());
                        TempPortal.m_EndPosition = new Point(br.ReadInt32(), br.ReadInt32());
                        TempPortal.m_szEnd = br.ReadString();
                        TempPortal.m_TriggerButton = br.ReadInt32();
                        mapViewer1.ObjectList.Add(TempPortal);
                    }
                    else if (TypeOfObject == "Enemy")
                    {
                        Enemy TempEnemy = new Enemy();
                        TempEnemy.Filename = br.ReadString();
                        TempEnemy.m_Position = new Point(br.ReadInt32(), br.ReadInt32());
                        mapViewer1.ObjectList.Add(TempEnemy);
                    }
                    else
                    {
                        MessageBox.Show("Unrecognized Object Type: " + TypeOfObject);
                    }
                }


                tempFile.Close();
                mapViewer1.Invalidate();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            mapViewer1.CurrentTool = MapViewer.Tool.Pen;
            toolStripButton1.Enabled = false;
            toolStripButton2.Enabled = true;
            toolStripButton3.Enabled = true;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            mapViewer1.CurrentTool = MapViewer.Tool.Eraser;
            toolStripButton1.Enabled = true;
            toolStripButton2.Enabled = false;
            toolStripButton3.Enabled = true;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            mapViewer1.CurrentTool = MapViewer.Tool.Fill;
            toolStripButton1.Enabled = true;
            toolStripButton2.Enabled = true;
            toolStripButton3.Enabled = false;
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mapViewer1.ChangeSize(new Size(0, 0));
            mapViewer1.ChangeSize(new Size(32, 32));
            numericUpDown1.Value = 32;
            numericUpDown2.Value = 32;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            mapViewer1.ShowGrid = !mapViewer1.ShowGrid;
            mapViewer1.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems[0].ToString() == "Portal")
            {
                PortalViewer tempPV = new PortalViewer();
                tempPV.Show();
            }
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex == 0)
            {
                mapViewer1.CurrentTool = SavedTool;
                toolStripButton1.Enabled = true;
                toolStripButton2.Enabled = true;
                toolStripButton3.Enabled = true;
                if (mapViewer1.CurrentTool == MapViewer.Tool.Pen)
                    toolStripButton1.Enabled = false;
                if (mapViewer1.CurrentTool == MapViewer.Tool.Eraser)
                    toolStripButton2.Enabled = false;
                if (mapViewer1.CurrentTool == MapViewer.Tool.Fill)
                    toolStripButton3.Enabled = false;
            }
            else if (e.TabPageIndex == 1)
            {
                SavedTool = mapViewer1.CurrentTool;
                toolStripButton1.Enabled = false;
                toolStripButton2.Enabled = false;
                toolStripButton3.Enabled = false;
                mapViewer1.CurrentTool = MapViewer.Tool.Object;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void mapViewer1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mapViewer1.HoveringObject != null)
            {
                toolStripStatusLabel1.Text = mapViewer1.HoveringObject.ToString();
            }
            else
            {
                toolStripStatusLabel1.Text = "";
            }

        }

        private void enemyMakerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EnemyMaker tempWind = new EnemyMaker();
            tempWind.Show();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
                mapViewer1.SelectedObject = listView1.SelectedItems[0].Text;
        }

        private void itemMakerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ItemMaker tempWind = new ItemMaker();
            tempWind.Show();
        }



    }
}
